﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WXApi.Model;
using WxTenpay.Common;

namespace WXApi.Controllers
{
    /// <summary>
    ///WangEditor 编辑器，图片和视频上传接口 
    /// </summary>
    /// 
    [Route("[controller]/[action]")]
    [ApiController]
    public class WangEditorApiController : ReqDataControllerBase
    {
        /// <summary>
        ///  wangeditor文件地址
        /// </summary>
        private string personpath = "/wwwwroot/file/wangeditor";

        #region WangEditor
        #region  接收编辑器图片接口
        /// <summary>
        /// 接收图片接口
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult updateimages(List<IFormFile> files)
        {
            try
            {

                foreach (var itm in files)
                {
                    if (itm.Length > 0)
                    {
                        var pathfile = Thumbnail.UploadFile(itm, personpath + "/images/" + DateTime.Now.ToString("yyyyMMdd"), DateTime.Now.ToString("yyyyMMddHHmmss"));
                        if (string.IsNullOrWhiteSpace(pathfile))
                        {
                            var error = new
                            {
                                errno = "文件异常，请重新上传文件！",
                                data = new List<object>(),

                            };
                            return Ok(error);
                        }
                    }

                }
                var errordata = new
                {
                    errno = "文件异常，请重新上传文件！",
                    data = new List<object>(),

                };
                return Success(errordata);
            }
            catch (Exception ex)
            {
                var errordata = new
                {
                    errno = ex.ToString(),
                    data = new List<object>(),
                };
                return Error(errordata);
            }
        }
        #endregion

        #region 接收编辑器视频接口
        /// <summary>
        /// 视频接口
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult updatevideo(List<IFormFile> files)
        {
            try
            {
                var data = new List<object>();
                foreach (var itm in files)
                {

                    if (itm.Length > 0)
                    {
                        var pathfile = Thumbnail.UploadFile(itm, personpath + "/video/" + DateTime.Now.ToString("yyyyMMdd"), DateTime.Now.ToString("yyyyMMddHHmmss"));
                        if (string.IsNullOrWhiteSpace(pathfile))
                        {
                            var error = new
                            {
                                errno = "文件异常，请重新上传文件！",
                                data = new List<object>(),

                            };
                            return Error(error);
                        }
                        data.Add(new
                        {
                            url = pathfile,
                            alt = "",
                            href = ""
                        });

                    }
                }
                var resdata = new
                {
                    errno = 0,
                    data

                };
                return Success(resdata);
            }
            catch (Exception ex)
            {
                var errordata = new
                {
                    errno = ex.ToString(),
                    data = new List<object>(),
                };
                return Error(errordata);
            }
        }
        #endregion

        #endregion


    }
}
